<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="40%"
    :append-to-body="true"
    :close-on-click-modal="false"
    @close="handleColse">
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="80px"
      size="small">
      <el-form-item
        label="部门全称"
        prop="fullName">
        <el-input v-model="ruleForm.fullName" placeholder="请输入部门全称"></el-input>
      </el-form-item>

      <el-form-item label="部门简称" prop="simpleName">
        <el-input v-model="ruleForm.simpleName" placeholder="请输入部门简称"></el-input>
      </el-form-item>

      <el-form-item label="上级部门">
        <treeselect
          v-model="ruleForm.pid"
          :clearable="true"
          placeholder="请选择上级部门"
          noChildrenText="不好意思哟~,没有下一级了~~"
          :normalizer="normalizer"
          :options="options"/>
          <!-- <el-select v-model="deptName" placeholder="请选择上级部门" style="width:100%" ref="selectReport">
                <el-option :value="ruleForm.pid" :label="deptName" style="width:100%;height:200px;overflow: auto;background-color:#fff">
                    <el-tree
                            :data="options"
                            :props="deptTreeProps"
                            @node-click="handleTreeSelectChange"
                    ></el-tree>
                </el-option>
            </el-select> -->
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="部门类型" prop="type">
            <el-select
              v-model="ruleForm.type"
              placeholder="请选择部门类型">
                <el-option
                  v-for="item in typeOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort"
            style="margin-left: 0; float: left;">
            <el-input-number
              v-model="ruleForm.sort"
              controls-position="right"
              :min="1"
              :max="1000000"
              label="描述文字"></el-input-number>
          </el-form-item>
        </el-col>
      </el-row>

      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="ruleForm.description"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="dialogVisible = false">取 消</el-button>
      <el-button size="small" type="primary" @click="submitData">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  getDataList,
  getListById,
  addList,
  editList
} from '@/api/auth/dept.js'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
export default {
  data() {
    return {
      // id: '',
      title: '新增信息',
      deptId: '',
      dialogVisible: false,
      ruleForm: {
        fullName: '',
        simpleName: '',
        pid: null,
        type: '',
        sort: 1,
        description: ''
      },
      rules: {
        fullName: [
          { required: true, message: '请输入部门全称', trigger: 'blur' }
        ],
        simpleName: [
          { required: true, message: '请输入部门简称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择部门类型', trigger: 'blur' }
        ]
      },
      options: [],
      normalizer(node) {
        return {
          label: node.fullName
        }
      },
   //    deptName: '',
			// deptTreeProps: {
			// 	label: 'fullName',
			// 	children: 'children'
			// },
      typeOptions: [
        { value: 1, label: '部门' },
        { value: 2, label: '公司' }
      ]
    }
  },
  components: {
    Treeselect
  },
  methods: {
    init(flag, id) {
      this.deptId = id
      if (id !== '') {
        this.title = '修改信息'
        this.getList()
        this.getInfoByid()
      } else {
        this.title = '新增信息'
        this.getList()
        this.ruleForm.fullName = ''
        this.ruleForm.simpleName = ''
        this.ruleForm.pid = null
        this.ruleForm.type = ''
        this.ruleForm.sort = 1
        this.ruleForm.description = ''
      }
      this.dialogVisible = flag
    },
    // 部门树选中
    // handleTreeSelectChange(data, node) {
    //   this.ruleForm.pid = data.id;
    //   this.deptName = data.fullName;
    // },
    // 获取部门树的数据
    getList() {
      getDataList().then((res) => {
        this.options = res.data
      })
    },
    // 修改时，根据id查询对应数据
    getInfoByid() {
      getListById(this.deptId).then((res) => {
        if (res.data.pid === '0') {
          this.ruleForm.fullName = res.data.fullName,
          this.ruleForm.simpleName = res.data.simpleName,
          this.ruleForm.pid = null,
          this.ruleForm.type = res.data.type,
          this.ruleForm.sort = res.data.sort,
          this.ruleForm.description = res.data.description
        } else {
          this.ruleForm = res.data
        }
      })
    },
    // 提交表单
    submitData() {
      this.$refs.ruleForm.validate((valid) => {
        if(!valid) return
        this.$confirm('确认提交吗？', '提示', {}).then(() => {
          if (this.deptId === '') {
            // 新增
            const addData = {
              pid: this.ruleForm.pid,
              type: this.ruleForm.type,
              fullName: this.ruleForm.fullName,
              simpleName: this.ruleForm.simpleName,
              description: this.ruleForm.description,
              sort: this.ruleForm.sort
            }
            addList(addData).then((res) => {
              this.$emit('handleList')
              this.dialogVisible = false
            })
          } else {
            // 修改
            const data = {
              id: this.deptId,
              deptId: this.deptId,
              pid: this.ruleForm.pid,
              type: this.ruleForm.type,
              fullName: this.ruleForm.fullName,
              simpleName: this.ruleForm.simpleName,
              description: this.ruleForm.description,
              sort: this.ruleForm.sort
            }
            editList(data).then((res) => {
              this.$emit('handleList')
              this.dialogVisible = false
            })
          }
        })
      })
    },
    handleColse() {
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style scoped>
  .el-select {
    width: 100%;
  }
  .el-select .el-option {
    height: 100%;
  }
</style>
